                                                                                                            
// 引入api文件  直接找里面的default
const api = require("../../utils/api.js").default;
// pages/gooddetail/gooddetail.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    gallery:[],
    info:[],
    attribute:[],
    productList:[],
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    arr:[],
    index:0,
    
  },
  
  bindPickerChange: function (e) { 
    if (e.detail.value != undefined){
      this.data.index = e.detail.value
      this.setData({
        index: this.data.index
      })
    }
    animationData:{}
  },
  bindPickerChange: function (e) {
    console.log(this.data.arr)

    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
      
    })
  },

  // 返回首页函数
  gohome(){
    wx.switchTab({
      url: "../../pages/home/home",
    })
  },
  // 改变收藏样式
  changeflag(){ 
   this.data.flag = !this.data.flag
    this.setData({
      flag: this.data.flag
    })
    if(this.data.flag){
      const token = wx.getStorageSync("token");
      var k = JSON.stringify(this.data.info.id)
      console.log(k)
      wx.request({
        url: api.addcollect,
        header: {
          //  请求头
          "content-type": "application/json", //默认  
          "X-Nideshop-Token": token
        },
        method: "POST",
        data: {
          typeId: 0,
          valueId:k
         
        },
        success: function (res) {
          console.log(res);
        }

      })
    }
  },
  // 跳转到购物车
  gocar(){
    wx.switchTab({
      url: "../../pages/shopcar/shopcar",
    })
  },
  // 跳转到购物确认页面
  gobuydetail(e){
    console.log(e)
    var buyid=e.currentTarget.dataset.buyid;
    var price = e.currentTarget.dataset.price;
    var num=++this.data.index

     wx.navigateTo({
       url:"../order/order"+"?id="+buyid+"&num="+num+"&price="+price
     })


  },

  // 加入购物车
  addcar(e){
    console.log(e) 
    var goodsId = e.currentTarget.dataset.shopid;
    var num=++this.data.index;
    var productId = e.currentTarget.dataset.productid;
    const token = wx.getStorageSync("token");
    wx.request({
      url: api.shopcar,
      header: {
        //  请求头
        "content-type": "application/json", //默认  
        "X-Nideshop-Token": token
      },
      method:"POST",
      data:{
        goodsId: goodsId,
        number: num,
        productId: productId
      },
      success:function(res){
        console.log(res);
      }

    })
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
    })

    this.animation = animation

    animation.opacity(1).step()

    this.animation = animation

    animation.opacity(0).step()

    this.setData({
      animationData: animation.export()
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


    var arr2 = []
    for (var i = 1; i <= 99; i++) {
      arr2.push(i)
    }
    this.setData({
      arr: arr2
    })
    this.bindPickerChange();


    // 请求商品详情信息
    var self=this
      console.log(options)
      wx.request({
        url: api.detail + "?id=" + options.id,
        success:function(res){
          console.log("b",res)
          self.setData({
            gallery: res.data.data.gallery,
            info: res.data.data.info,
            attribute: res.data.data.attribute,
            productList:res.data.data.productList
          })
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})